﻿

using IotContract.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IotContract.Models.Clients.Hks;
/// <summary>
/// 充放电配方
/// </summary>

public class ChargeStartData
{

    public string StageNo { get; set; }

    /// <summary>
    /// TypeNo P102
    /// </summary>

    public string RouterCode { get; set; }
    /// <summary>
    /// ProcNo
    /// </summary>
    public int StepId { get; set; }


    public string LotNO { get; set; }
    public string TrayNo { get; set; }
    /// <summary>
    /// FMT(Charge Discharge), PRE(Pre Charge), DCR
    /// </summary>
    public string StepCode { get; set; }



    /// <summary>
    /// 预化成
    /// </summary>
    public FMTForm FormVo { get; set; }


    public List<FMTCell> Calllist { get; set; } = new List<FMTCell>();





    public override string ToString()
    {

        if (FormVo == null || FormVo.YhcVo == null || FormVo.YhcVo.steps == null || FormVo.YhcVo.steps.Count == 0)
        {
            return string.Empty;
        }

        string stepData = string.Empty;



        FormVo.YhcVo.YhcMinimumVoltageChange = "4200";



        foreach (var step in FormVo.YhcVo.steps)
        {
            stepData += step.StepCnt.ToString().ToSafe().PadLeft(2, ' ');
            stepData += step.ChargeDischarge.ToString().ToSafe().PadLeft(1, ' ');
            stepData += step.ConstantPressure.ToString().ToSafe().PadLeft(7, ' ');
            stepData += step.ConstantCurrent.ToString().ToSafe().PadLeft(7, ' ');
            stepData += step.ChargeTime.ToString().ToSafe().PadLeft(7, ' ');
            stepData += step.EndVoltage.ToString().ToSafe().PadLeft(7, ' ');
            stepData += step.EndCurrent.ToString().ToSafe().PadLeft(7, ' ');
            stepData += step.EndCapacity.ToString().ToSafe().PadLeft(7, ' ');
            stepData += step.InspectionTime.ToString().ToSafe().PadLeft(5, ' ');
        }


        #region Step Setting


        string stepSetting = string.Empty;
        stepSetting += FormVo.YhcVo.YhcChargingVoltageUpper.ToSafe().ToSafe().PadLeft(4, ' ');
        stepSetting += FormVo.YhcVo.YhcChargingCapacityUpper.ToSafe().PadLeft(4, ' ');
        stepSetting += FormVo.YhcVo.YhcChargingCurrentLopper.ToSafe().PadLeft(4, ' ');
        stepSetting += FormVo.YhcVo.YhcChargingVoltageLowerTime.ToSafe().PadLeft(5, ' ');
        stepSetting += FormVo.YhcVo.YhcChargingVoltageLowerchack.ToSafe().PadLeft(4, ' ');
        stepSetting += FormVo.YhcVo.YhcVisualInspectionT1Time.ToSafe().PadLeft(5, ' ');
        stepSetting += FormVo.YhcVo.YhcVisualVoltageT1Upper.ToSafe().PadLeft(4, ' ');

        stepSetting += FormVo.YhcVo.YhcVisualVoltageT1Lower.ToSafe().PadLeft(4, ' ');
        stepSetting += FormVo.YhcVo.YhcVisualInspectionT2Time.ToSafe().PadLeft(5, ' ');

        stepSetting += FormVo.YhcVo.YhcVisualVoltageT2Upper.ToSafe().PadLeft(4, ' ');
        stepSetting += FormVo.YhcVo.YhcVisualVoltageT2Lower.ToSafe().PadLeft(4, ' ');

        stepSetting += FormVo.YhcVo.YhcTimeT3.ToSafe().PadLeft(5, ' ');
        stepSetting += FormVo.YhcVo.YhcTimeT4.ToSafe().PadLeft(5, ' ');
        stepSetting += FormVo.YhcVo.YhcVisualInspectionT5Time.ToSafe().PadLeft(5, ' ');


        stepSetting += FormVo.YhcVo.YhcVisualVoltageT5Upper.ToSafe().PadLeft(4, ' ');
        stepSetting += FormVo.YhcVo.YhcVisualVoltageT5Lower.ToSafe().PadLeft(4, ' ');
        stepSetting += FormVo.YhcVo.YhcConstantChargeVoltageDeviation.ToSafe().PadLeft(4, ' ');
        stepSetting += FormVo.YhcVo.YhcNumOfChargingIncreases.ToSafe().PadLeft(3, ' ');
        stepSetting += FormVo.YhcVo.YhcNumOfChargingCurrentIncreases.ToSafe().PadLeft(3, ' ');

        stepSetting += FormVo.YhcVo.YhcNumOfChargingPressureDrops.ToSafe().PadLeft(3, ' ');

        stepSetting += FormVo.YhcVo.YhcNumOfChargingContinuousDrops.ToSafe().PadLeft(3, ' ');



        stepSetting += FormVo.YhcVo.YhcChargingTimeout.ToSafe().PadLeft(5, ' ');

        stepSetting += FormVo.YhcVo.YhcDischargeVoltageLower.ToSafe().PadLeft(4, ' ');

        stepSetting += FormVo.YhcVo.YhcDischargeTimeout.ToSafe().PadLeft(5, ' ');


        stepSetting += FormVo.YhcVo.YhcDischargeCapacitySetpoint.ToSafe().PadLeft(4, ' ');


        stepSetting += FormVo.YhcVo.ReleaseVoltageRecovery.ToSafe().PadLeft(3, ' ');

        stepSetting += FormVo.YhcVo.YhcNumOfDischargeVoltageRises.ToSafe().PadLeft(3, ' ');

        stepSetting += FormVo.YhcVo.YhcConstantCurrentFluctuation.ToSafe().PadLeft(4, ' ');


        stepSetting += FormVo.YhcVo.YhcExcessiveCurrent.ToSafe().PadLeft(4, ' ');

        stepSetting += FormVo.YhcVo.YhcSleepCurrent.ToSafe().PadLeft(4, ' ');


        stepSetting += FormVo.YhcVo.YhcExcessiveVoltage.ToSafe().PadLeft(4, ' ');

        stepSetting += FormVo.YhcVo.YhcLineVoltage.ToSafe().PadLeft(4, ' ');

        stepSetting += FormVo.YhcVo.YhcDischargePressureDifferenceTime.ToSafe().PadLeft(4, ' ');

        stepSetting += FormVo.YhcVo.YhcSleepVoltageDifferenceTime.ToSafe().PadLeft(4, ' ');

        stepSetting += FormVo.YhcVo.YhcContactResistance.ToSafe().PadLeft(4, ' ');

        stepSetting += FormVo.YhcVo.YhcVoltageMutation.ToSafe().PadLeft(2, ' ');

        stepSetting += FormVo.YhcVo.YhcAttentionTemperature.ToSafe().PadLeft(2, ' ');


        stepSetting += FormVo.YhcVo.YhcCellTemperature.ToSafe().PadLeft(2, ' ');
        stepSetting += FormVo.YhcVo.YhcCurrentPercentage.ToSafe().PadLeft(3, ' ');

        stepSetting += FormVo.YhcVo.YhcInitialVoltageUpper.ToSafe().PadLeft(4, ' ');

        stepSetting += FormVo.YhcVo.YhcInitialVoltageLower.ToSafe().PadLeft(4, ' ');


        stepSetting += FormVo.YhcVo.YhcCompleteVoltageUpper.ToSafe().PadLeft(4, ' ');

        stepSetting += FormVo.YhcVo.YhcCompleteVoltageLower.ToSafe().PadLeft(4, ' ');

        stepSetting += FormVo.YhcVo.YhcOneDischargeCapacityUpper.ToSafe().PadLeft(4, ' ');
        stepSetting += FormVo.YhcVo.YhcOneDischargeCapacityLower.ToSafe().PadLeft(4, ' ');

        stepSetting += FormVo.YhcVo.YhcTwoDischargeCapacityUpper.ToSafe().PadLeft(4, ' ');
        stepSetting += FormVo.YhcVo.YhcTwoDischargeCapacityLower.ToSafe().PadLeft(4, ' ');



        #endregion


        #region Contract Setting

        string contractData = string.Empty;

        contractData += FormVo.YhcVo.YhcChargingVoltage.ToSafe().PadLeft(5, ' ');

        contractData += FormVo.YhcVo.YhcChargingCurrent.ToSafe().PadLeft(4, ' ');

        contractData += FormVo.YhcVo.YhcStartTime.ToSafe().PadLeft(2, ' ');


        contractData += FormVo.YhcVo.YhcChargingCutoffTime.ToSafe().PadLeft(2, ' ');

        contractData += FormVo.YhcVo.YhcVoltageDetection.ToSafe().PadLeft(5, ' ');

        contractData += FormVo.YhcVo.YhcReverseVoltage.ToSafe().PadLeft(5, ' ');

        contractData += FormVo.YhcVo.YhcVoltageUpperLimit.ToSafe().PadLeft(5, ' ');

        contractData += FormVo.YhcVo.YhcVoltageLower.ToSafe().PadLeft(5, ' ');

        contractData += FormVo.YhcVo.YhcVoltageChangeMax.ToSafe().PadLeft(5, ' ');

        contractData += FormVo.YhcVo.YhcMinimumVoltageChange.ToSafe().PadLeft(5, ' ');

        contractData += FormVo.YhcVo.YhcCurrentDeviation.ToSafe().PadLeft(3, ' ');
        contractData += FormVo.YhcVo.YhcNumberAlarms.ToSafe().PadLeft(3, ' ');



        #endregion

        string cellData = string.Empty;

        foreach (var cell in Calllist)
        {
            cellData += cell.Results.ToString().ToSafe().PadLeft(1, ' ');
        }

        string firstData = StageNo.ToSafe().PadLeft(4, ' ') + RouterCode.ToSafe().PadLeft(4, ' ') + StepId.ToString().ToSafe().PadLeft(2, '0') + LotNO.ToSafe().PadLeft(10, ' ') + TrayNo.ToSafe().PadLeft(7, ' ') + StepCode.ToSafe().PadLeft(3, ' ')
            + FormVo.YhcVo.steps.Count.ToString().ToSafe().PadLeft(2, ' ');

        int firstLenth = firstData.Length;

        int stepLenth = stepData.Length;

        int stepSettingLenth = stepSetting.Length;
        int contractDataLenth = contractData.Length;

        int cellDataLenth = cellData.Length;


        return firstData + stepData + stepSetting + contractData + cellData;

    }

}

public class FMTForm
{
    public PreFMT YhcVo { get; set; }
}

/// <summary>
/// 预化成
/// </summary>
public class PreFMT
{
    public List<FMTStep> steps { get; set; }
    #region Step Settings
    /// <summary>
    /// 1充电上限电压
    /// </summary>
    public string YhcChargingVoltageUpper { get; set; }
    /// <summary>
    /// 2充电上限容量
    /// </summary>
    public string YhcChargingCapacityUpper { get; set; }
    /// <summary>
    /// 3充电电流下限
    /// </summary>
    public string YhcChargingCurrentLopper { get; set; }
    /// <summary>
    /// 4充电下限电压检查时间
    /// </summary>
    public string YhcChargingVoltageLowerTime { get; set; }
    /// <summary>
    /// 5充电下限电压检查
    /// </summary>
    public string YhcChargingVoltageLowerchack { get; set; }
    /// <summary>
    /// 6T1时刻检查时间
    /// </summary>
    public string YhcVisualInspectionT1Time { get; set; }
    /// <summary>
    /// 7 T1时刻电压上限
    /// </summary>
    public string YhcVisualVoltageT1Upper { get; set; }
    /// <summary>
    /// 8 T1时刻电压下限
    /// </summary>
    public string YhcVisualVoltageT1Lower { get; set; }
    /// <summary>
    /// T2时刻检查时间
    /// </summary>
    public string YhcVisualInspectionT2Time { get; set; }
    /// <summary>
    /// 9 T2时刻电压上限
    /// </summary>
    public string YhcVisualVoltageT2Upper { get; set; }
    /// <summary>
    /// 10 T2时刻电压下限
    /// </summary>
    public string YhcVisualVoltageT2Lower { get; set; }
    /// <summary>
    /// 11 T3小时
    /// </summary>
    public string YhcTimeT3 { get; set; }
    /// <summary>
    ///  12 T4小时
    /// </summary>
    public string YhcTimeT4 { get; set; }
    /// <summary>
    /// 13 T5检查时间
    /// </summary>
    public string YhcVisualInspectionT5Time { get; set; }
    /// <summary>
    /// T5时刻电压上限
    /// </summary>
    public string YhcVisualVoltageT5Upper { get; set; }
    /// <summary>
    /// 14 T5时刻电压下限
    /// </summary>
    public string YhcVisualVoltageT5Lower { get; set; }
    /// <summary>
    /// 15 恒定充电电压偏差
    /// </summary>
    public string YhcConstantChargeVoltageDeviation { get; set; }
    /// <summary>
    /// 16充电电流上升累计次数
    /// </summary>
    public string YhcNumOfChargingIncreases { get; set; }
    /// <summary>
    /// 17 充电电流上升连续次数
    /// </summary>
    public string YhcNumOfChargingCurrentIncreases { get; set; }
    /// <summary>
    /// 18 充电电压下降累计次数
    /// </summary>
    public string YhcNumOfChargingPressureDrops { get; set; }
    /// <summary>
    /// 19 充电电压下降连续次数
    /// </summary>
    public string YhcNumOfChargingContinuousDrops { get; set; }
    /// <summary>
    /// 20 充电时间超时
    /// </summary>
    public string YhcChargingTimeout { get; set; }
    /// <summary>
    /// 放电电压下限
    /// </summary>
    public string YhcDischargeVoltageLower { get; set; }
    /// <summary>
    /// 21 放电时间超时
    /// </summary>
    public string YhcDischargeTimeout { get; set; }
    /// <summary>
    /// 22 放电容量设定信息保护值
    /// </summary>
    public string YhcDischargeCapacitySetpoint { get; set; }
    /// <summary>
    /// 23 放电电压上升累计回收
    /// </summary>
    public string ReleaseVoltageRecovery { get; set; }
    /// <summary>
    /// 24 放电电压上升连续次数
    /// </summary>
    public string YhcNumOfDischargeVoltageRises { get; set; }
    /// <summary>
    /// 25 恒定电流波动
    /// </summary>
    public string YhcConstantCurrentFluctuation { get; set; }
    /// <summary>
    /// 26 电流过大
    /// </summary>
    public string YhcExcessiveCurrent { get; set; }
    /// <summary>
    /// 27 休眠时电路电流
    /// </summary>
    public string YhcSleepCurrent { get; set; }
    /// <summary>
    /// 28 电压过大
    /// </summary>
    public string YhcExcessiveVoltage { get; set; }
    /// <summary>
    /// 29 电流线电压过大
    /// </summary>
    public string YhcLineVoltage { get; set; }
    /// <summary>
    /// 30 放电时间电压差
    /// </summary>
    public string YhcDischargePressureDifferenceTime { get; set; }
    /// <summary>
    /// 31 休眠时间电压差
    /// </summary>
    public string YhcSleepVoltageDifferenceTime { get; set; }
    /// <summary>
    /// 32 接触电阻
    /// </summary>
    public string YhcContactResistance { get; set; }
    /// <summary>
    /// 33 电压突变
    /// </summary>
    public string YhcVoltageMutation { get; set; }
    /// <summary>
    /// 34 注意温度
    /// </summary>
    public string YhcAttentionTemperature { get; set; }
    /// <summary>
    /// 35 电芯温度报警
    /// </summary>
    public string YhcCellTemperature { get; set; }
    /// <summary>
    /// 36 电流百分比
    /// </summary>
    public string YhcCurrentPercentage { get; set; }

    /// <summary>
    /// 37 初始电压上限
    /// </summary>
    public string YhcInitialVoltageUpper { get; set; }
    /// <summary>
    /// 38初始电压下限
    /// </summary>
    public string YhcInitialVoltageLower { get; set; }
    /// <summary>
    /// 39 完成电压上限
    /// </summary>
    public string YhcCompleteVoltageUpper { get; set; }
    /// <summary>
    /// 40 完成电压下限
    /// </summary>
    public string YhcCompleteVoltageLower { get; set; }
    /// <summary>
    /// 41 一次放电容量上限
    /// </summary>
    public string YhcOneDischargeCapacityUpper { get; set; }
    /// <summary>
    /// 42 一次放电容量下限
    /// </summary>
    public string YhcOneDischargeCapacityLower { get; set; }
    /// <summary>
    /// 43 二次放电容量上限
    /// </summary>
    public string YhcTwoDischargeCapacityUpper { get; set; }
    /// <summary>
    /// 44 二次放电容量下限
    /// </summary>
    public string YhcTwoDischargeCapacityLower { get; set; }
    #endregion



    /// <summary>
    /// 1 充电电压
    /// </summary>
    public string YhcChargingVoltage { get; set; }
    /// <summary>
    /// 2 充电电流
    /// </summary>
    public string YhcChargingCurrent { get; set; }
    /// <summary>
    /// 3 时间检测
    /// </summary>
    public string YhcStartTime { get; set; }
    /// <summary>
    /// 4 充电截止时间
    /// </summary>
    public string YhcChargingCutoffTime { get; set; }
    /// <summary>
    /// 5 电压检测
    /// </summary>
    public string YhcVoltageDetection { get; set; }
    /// <summary>
    /// 6 反向电压
    /// </summary>
    public string YhcReverseVoltage { get; set; }
    /// <summary>
    /// 7 电压上限值
    /// </summary>
    public string YhcVoltageUpperLimit { get; set; }

    /// <summary>
    /// 8 电压下限
    /// </summary>
    public string YhcVoltageLower { get; set; }
    /// <summary>
    /// 9 电压最大变化
    /// </summary>
    public string YhcVoltageChangeMax { get; set; }
    /// <summary>
    /// 10 电压最小变化
    /// </summary>
    public string YhcMinimumVoltageChange { get; set; }
    /// <summary>
    /// 电流偏差
    /// </summary>
    public string YhcCurrentDeviation { get; set; }
    /// <summary>
    /// 报警发生个数
    /// </summary>
    public string YhcNumberAlarms { get; set; }


}

public class FMTStep
{
    public int StepCnt { get; set; }
    /// <summary>
    /// 1 skip 2 rest 3 cc 4 cv 5ccc 6dc
    /// </summary>
    public double ChargeDischarge { get; set; }
    /// <summary>
    /// 恒压
    /// </summary>
    public double ConstantPressure { get; set; }
    /// <summary>
    /// 恒电流
    /// </summary>
    public double ConstantCurrent { get; set; }
    /// <summary>
    /// 时间
    /// </summary>
    public double ChargeTime { get; set; }
    /// <summary>
    /// 终止电压
    /// </summary>
    public double EndVoltage { get; set; }
    /// <summary>
    /// 终止电流
    /// </summary>
    public double EndCurrent { get; set; }
    /// <summary>
    /// 终止容量
    /// </summary>
    public double EndCapacity { get; set; }
    /// <summary>
    /// 限制检验时间
    /// </summary>
    public double InspectionTime { get; set; }






}

/// <summary>
/// 电芯
/// </summary>
[ProtoBuf.ProtoContract]
public class FMTCell
{
    /// <summary>
    /// 0 : OK, 1 : 之前工程不良, 2 : No Cell
    /// </summary>
    [ProtoBuf.ProtoMember(1)]
    public int Results { get; set; }
    [ProtoBuf.ProtoMember(2)]
    public int Channel { get; set; }
}
